www.gusucode.com > VC++ 模拟超市POS机-源码程序 > VC++ 模拟超市POS机-源码程序/code/POS机/StoreBase.cpp

    //Download by http://www.NewXing.com
// StoreBase.cpp: implementation of the StoreBase class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StoreBase.h"
#include <string.h>

#define MAX_SIZE 1000

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

StoreBase::StoreBase()
{

}

StoreBase::~StoreBase()
{

}

bool StoreBase::open(char* filename,char* charity)
{
	fp=fopen(filename,charity);
    if(fp == NULL)
	{
		return false;
	}
    else
	{
		return true;
	}
}

void StoreBase::close()
{ 
    if(fp != NULL)
    {
	    fclose(fp);
    }
}

bool StoreBase::check(char* pid)
{
    int i;
   
    fseek(fp,0,2);
    int end = ftell(fp);
    fseek(fp,0,0);
    int size = end / sizeof(Product);
    Product pro[MAX_SIZE];
    for(i=0; i < size; i++)
	{
        fread(&pro[i], sizeof(Product),1,fp);
	}

    for(i=0;i<size;i++)
	{
		if(strcmp(pid, pro[i].getId())==0)
		{
			pro[i].display();
			return true;
		}
	}

	return false;
}

void StoreBase::list()
{
	int i;
   
    fseek(fp,0,2);
    int end = ftell(fp);
    fseek(fp,0,0);
    int size = end / sizeof(Product);
    Product pro[MAX_SIZE];
    for(i=0; i < size; i++)
	{
        fread(&pro[i], sizeof(Product),1,fp);
	}

    for(i=0;i<size;i++)
	{
		pro[i].display();
	}
}

void StoreBase::add(Product &p)
{	
	fwrite(&p, sizeof(Product), 1, fp);
}